/**
 *
 * @file webserver.h
 *
 * @brief Header file for built in webserver
 * 
 * $Id: webserver.h 123 2009-01-15 08:42:49Z henri.doreau $
 */

/*
 * This file is part of Tsar.
 *
 * Tsar is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tsar is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Tsar.  If not, see <http://www.gnu.org/licenses/>.
 */


#ifndef WEBSERVER_H
#define WEBSERVER_H


/* -- S T A N D A R D  I N C L U D E S -- */
#include <time.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>

/* -- N E T W O R K I N G  I N C L U D E S -- */
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

/* -- L O C A L  A P I  I N C L U D E S -- */
#include <loop.h>
#include <output.h>
#include <options.h>
#include <sockpool.h>

/* -- C O N S T A N T S -- */
/** @def maximum length to capture from network in one recv */
#define SNAPLEN         2048
/** @brief number of msec to idle after a call to ioctl */
#define IOCTL_TIME_MSEC 100
/** number of usec to idle after a call to ioctl */
#define IOCTL_IDLE_USEC ((IOCTL_TIME_MSEC) * 1000 )


/* -- T Y P E D E F S -- */



/* -- F U N C T I O N S  P R O T O T Y P E S -- */
/**
 * @brief startup built in webserver, that will listen for an incoming connection
 * @param o user options values
 * @return error code according to the execution of the function
 * @retval 0 no error occured
 * @retval 1 cannot bind the server
 * @retval 2 cannot listen for incoming xonnections
 */
int start_webserver( tsar_t * session, opt_t * o );

/**
 * @brief attach server to an address
 * @param o user options
 * @param params internal parameters
 * @return error code according to the execution of the function
 * @retval 0 no error occured
 * @retval 1 getaddrinfo failed to find out free addresses
 * @retval 2 cannot bind the server to any of the addresses found
 */
int bind_server( opt_t * o, int * sockFd );

/**
 * @brief try to open a socket that will be the one listening for incoming connections
 * @param res is an addrinfo structure where are stored parametes (family, type, protocol) for possible openings
 * @param httpPort is a null terminated string that describes the port onto which the socket has to be bound
 * @return the opened socket fileno
 * @retval -1 (invalid socket) cannot open socket
 * @retval >0 socket opened
 */
int open_socket( struct addrinfo * res, const char * httpPort );

/**
 * @brief turn the http server off
 * @param params internal parameters
 */
void shutdown_webserver( tsar_t * session );

/**
 * @brief sockpool callback : accept an incoming connection
 * @param sockFd, the socket on which the incoming connection has been detected
 * @param datas is a tsar_t casted pointer to the current session
 */
void webserver_accept( int sockFd, void * datas );

/**
 * @brief sockpool callback : process an incoming (http?) request
 * @param sockFd, the socket on which the incoming datas has been detected
 * @param datas is a tsar_t casted pointer to the current session
 */
void webserver_http_reply( int sockFd, void * datas );

/**
 * @brief remove a closed connection from the session's list of opened connections
 * @param sockFd, the socket the connection was established on
 * @param session the current session
 */
void unregister_closed_connection( int sockFd, tsar_t * session );

/**
 * @brief unblock socket and wait for ioctl to be applied
 */
void set_sock_unblock( int fd );

/**
 * @brief block socket and wait for ioctl to be applied
 */
void set_sock_block( int fd );

#endif /* WEBSERVER_H */

